﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;

namespace YouHP.Pages.SymptomHistory
{
    public class SymptomAjax : YouHP.Pages.BasePage
    {

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            if (IsPost == false) HandleGet();
        }

        protected void HandleGet()
        {

            
            int symid = RequestTools.GetQueryInt("symid", 0);
            if (symid <= 0)
            {
                return;
            }

            SymptomInfo symdata = SymptomBLL.Instance.GetSymptomInfo(symid);
            if (symdata.Id <= 0)
            {
                return;
            }
            
            if (UserData.Id <= 0) return;
            PatientRecordInfo prData = PatientBLL.Instance.GetPatientRecordInfo(UserData.Id);
            if (prData.Id <= 0) return;

            int count = 0;
            List<SymptomHistoryInfo> lst = PatientSymptomBLL.Instance.GetSymptomHistoryList( prData.Id, symid,10000, 1, out count);


            MakeGraphData(prData, symdata,lst);
        }

        private void MakeGraphData(PatientRecordInfo prData, SymptomInfo symdata, List<SymptomHistoryInfo> lst)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("&SingleSymptom=1&\r\n");
            sb.AppendFormat("&StartDate={0:yyyy-MM-dd}&\r\n", prData.StartDate);
            sb.AppendFormat("&DosageDate={0:yyyy-MM-dd}&\r\n", prData.ConfirmDate);
            sb.AppendFormat("&UpdateDate={0:yyyy-MM-dd}&\r\n", prData.UpdateDate);

            sb.AppendFormat("&Var={0};{1}&\r\n", symdata.Name, lst.Count);
            int cnt = lst.Count - 1;
            for (int i = 0; i <=cnt; i++)
            {
                SymptomHistoryInfo info = lst[cnt - i];
                sb.AppendFormat("&Data_{0}={1:yyyy-MM-dd};{2}&\r\n",
                    i, 
                    info.RecordDate,
                    info.DegreeValue
                    );
            }

            ResponseXML(sb);
        }

        private void ResponseXML(StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/html";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }
    }
}